package com.kehutong.charge.admin;

import com.kehutong.charge.entity.ChargeBillHouse;
import com.kehutong.charge.entity.ChargeBillCommunity;
import com.kehutong.charge.service.ChargeBillManageService;
import com.kehutong.common.DeptService;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: kht
 * @description: 账单管理
 * @author: joneron
 * @create: 2021/6/23
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/bill/manage")
public class ChargeBillManageController {

    private static final Logger logger = LoggerFactory.getLogger(ChargeBillManageController.class);

    @Inject
    DeptService deptService;

    @Inject
    ChargeBillManageService chargeBillManageService;

    @ReqMapping("/page")
    @HasPermission("pay:charge:bill:mange:view")
    public Object page(JSONObject jsonObject) throws Exception {
        return chargeBillManageService.findPage(jsonObject);
    }


    @ReqMapping("/page/export")
    @HasPermission("pay:charge:bill:mange:view")
    public void pageExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject){
        chargeBillManageService.pageExport(req,resp,jsonObject);
    }

    @ReqMapping("/list")
    @HasPermission("pay:charge:bill:mange:view")
    public Object list(JSONObject jsonObject) {
        return chargeBillManageService.findList(jsonObject);
    }

    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:bill:mange:edit")
    @ReqMapping("/save")
    public Object save(ChargeBillCommunity entity, Token token) throws Exception {
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());

            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }
        entity.setCreateName(token.getUsername());
        return chargeBillManageService.save(entity);
    }

    @Bean(copy=true)
    @HasPermission("pay:charge:bill:mange:edit")
    @ReqMapping("/update")
    public Object update(ChargeBillCommunity entity,Token token) throws Exception {

        entity.setUpdateName(token.getUsername());
        return chargeBillManageService.update(entity);
    }

    /**
     * 类目账单生成
     * @param entity
     * @param token
     * @return
     * @throws Exception
     */
    @Bean(copy=true)
    @HasPermission("pay:charge:bill:mange:edit")
    @ReqMapping("/generateItemBill")
    public Object generateItemBill(ChargeBillCommunity entity,Token token) throws Exception {

        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());

            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }
        entity.setCreateName(token.getUsername());

        return chargeBillManageService.generateItemBill(entity);
    }

    @ReqMapping("/get")
    public Object get(ChargeBillCommunity entity) {

        return Result.success(entity);
    }

    /**
     * 状态修改
     * @param entity
     * @return
     */
    @ReqMapping("/setStatus")
    @HasPermission("pay:charge:bill:mange:edit")
    public Object setStatus(ChargeBillHouse entity, Token token) {

        return Result.success();
    }

    /**
     * 重新生成账单
     * @param jsonObject
     * @return
     */
    @ReqMapping("/resetCommunityBill")
    @HasPermission("pay:charge:bill:mange:edit")
    public Object resetCommunityBill(JSONObject jsonObject, Token token) throws Exception {

        return chargeBillManageService.resetCommunityBill(jsonObject,token);
    }

    /**
     * 账单作废(单条或多条)
     * @param jsonObject
     * @return
     */
    @ReqMapping("/cancelCommunityBill")
    @HasPermission("pay:charge:bill:mange:edit")
    public Object cancelCommunityBill(JSONObject jsonObject, Token token) {

        return chargeBillManageService.cancelCommunityBill(jsonObject,token);
    }

    /**
     * 批量导入
     * @param
     * @return
     */
    @ReqMapping("/batchImport")
    @HasPermission("pay:charge:bill:mange:edit")
    public Object batchImport(JSONObject jsonObject, Token token) {

        return Result.success("暂未实现！");
    }

    /**
     * 批量导出
     * @param
     * @return
     */
    @ReqMapping("/batchExport")
    @HasPermission("pay:charge:bill:mange:edit")
    public Object batchExport(JSONObject jsonObject, Token token) {

        return Result.success("暂未实现！");
    }


    /**
     * 手动生成收据
     * @param jsonObject
     * @param token
     * @return
     * @throws Exception
     */
    @Bean(newInstance=true, copy=true)
    @ReqMapping("/manual/receipt/pdf")
    public Object receiptPDFSave(JSONObject jsonObject, Token token){
        return chargeBillManageService.receiptPDFSave(jsonObject,token);
    }

    /**
     * 手动生成缴费单
     * @param jsonObject
     * @param token
     * @return
     * @throws Exception
     */
    @Bean(newInstance=true, copy=true)
    @ReqMapping("/manual/payment/pdf")
    public Object manualPaySave(JSONObject jsonObject, Token token){
        return chargeBillManageService.manualPaySave(jsonObject);
    }

}
